package io.zhoujie.modules.app.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.zhoujie.common.utils.R;
import io.zhoujie.modules.app.entity.AppSignEntity;
import io.zhoujie.modules.app.service.AppSignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author zhoujie
 * @email 813685764@qq.com
 * @date 2024/3/15 22:58
 */
@RestController
@RequestMapping("app/sign")
@Api(tags = "用户端-日常签到")
public class AppSignController {
    @Resource
    private AppSignService appSignService;

    /**
     * 日常签到
     */
    @GetMapping("/signin")
    @ApiOperation("日常签到")
    public R signin(@RequestParam(value = "userId", required = true) Integer userId) {
        int sign = appSignService.insertSignIn(userId);
        return R.ok();
    }

    /**
     * 查询签到状态
     * @param userId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("签到状况")
    public R list(@RequestParam(value = "userId", required = true) Integer userId){
        List<AppSignEntity> list = appSignService.selectByUserId(userId);
        return R.ok().put("result",list);
    }

    /**
     * 查询签到排行
     * @param userId
     * @return
     */
    @GetMapping("/today")
    @ApiOperation("今日签到排行")
    public R today(@RequestParam(value = "userId", required = true) Integer userId){
        Integer top = appSignService.selectTopToday(userId);
        return R.ok().put("result",top);
    }
    /**
     * 签到列表
     */
    @GetMapping("/listSign")
    @ApiOperation("签到列表")
    public R listSign(@RequestParam("t") String t ,@RequestParam(value = "signDate", required = false) String signDate) throws ParseException {
        AppSignEntity sign = new AppSignEntity();
        if (StringUtils.isNotBlank(signDate)){
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            Date date = ft.parse(signDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            // 把日期往后增加一天,整数  往后推,负数往前移动
            calendar.add(Calendar.DATE, 1);
            // 这个时间就是日期往后推一天的结果
            date = calendar.getTime();
            sign.setSignDate(date);
        }
        List<AppSignEntity> list = appSignService.selectSignList(sign);
        return R.ok().put("result",list);
    }
    /**
     * 删除签到
     */
    @PostMapping("/delete")
    @ApiOperation("删除签到")
    public R delete(@RequestBody Integer[] ids){
        appSignService.deleteByUserId(ids);
        return R.ok();
    }
}
